
/*
 * @项目名称: 模板开发
 * @FilePath: /meek/src/geometry/squarex.js
 * @Author: kongxueyang@datatang.com
 * @Date: 2020-09-11T09:35:05+08:00
 * @LastEditors: kongxueyang@datatang.com
 * @LastEditTime: 2020-09-11T09:35:05+08:00
 * Copyright (c) 2018-2022 
 */
                                                    

import Geometry from './geometry'
import Polygon from './polygon'
import { deepClone } from './support/cubeutil'
import Feature from '../meek/feature';
import Line from './line';
import { Style } from './../style/style';

export default class SquareX extends Polygon {
  
  constructor(coordinates) {
    super(coordinates)
    this._referLineFeature = null;
  }

  get geometryType() {
    return Geometry.SQUAREX
  }

  set refLine(value) {
    if (value) {
      this.showRefLineCoords();
    } else {
      
    }
  }

  getNextPoint(coordinates) {
    if (coordinates.length === 2) {
      return this.getSecondPoint(coordinates);
    } else if (coordinates.length === 4) {
      // return this.getLastPoint(coordinates);
    } else {
      return;
    }
  }

  getSecondPoint(coordinates) {
    const first = coordinates[0];
    const second = coordinates[1];
    return [second[0], first[1]];
  }

  getLastPoint(coordinates) {
    const third = coordinates[2];
    const last = coordinates[3];
    return [third[0], last[1]];
  }

  getFormShowPosition() {
    let rings = this.getCoordinates();
    let max = 0;
    let maxIndex = 0;
    rings[0].forEach((xy, index) => {
      if (xy[0] > max) {
        maxIndex = index;
        max = xy[0];
      }
    })
    return rings[0][maxIndex]
  }

   /**
   * 重新计算顶点
   *
   * @param p 对象
   * @param vertex 新移动的点
   * @param oldVertex 上次移动的点
   * @param dragSegment 数据对象
   * @param extent 绘制范围
   * @returns {*} 返回新的点集合（3维数组）
   */
  static updateCoordinatesForModification(p, vertex, oldVertex, dragSegment, extent) {
    const oldCoordinates = deepClone(p.getCoordinates())
    const segment = dragSegment[0];
    const i = segment.index;
    if (typeof i == 'object') {
      const { index, ringIndex } = i;
      if (index == 0 || index == 1) {
        let ymin = Math.min(oldCoordinates[ringIndex][2][1], oldCoordinates[ringIndex][3][1]);
        if (vertex[1] >= ymin) {
          return ;
        }
      }
      if (index == 2 || index == 3) {
        let ymin = Math.min(oldCoordinates[ringIndex][0][1], oldCoordinates[ringIndex][1][1]);
        if (vertex[1] <= ymin) {
          return ;
        }
      }
      if (index == 0) {
        oldCoordinates[ringIndex][0] = vertex;
        oldCoordinates[ringIndex][1][1] = vertex[1];
        oldCoordinates[ringIndex][oldCoordinates[ringIndex].length - 1] = oldCoordinates[ringIndex][0];
      }
      if (index == 1) {
        oldCoordinates[ringIndex][1] = vertex;
        oldCoordinates[ringIndex][0][1] = vertex[1];
        oldCoordinates[ringIndex][oldCoordinates[ringIndex].length - 1] = oldCoordinates[ringIndex][0];
      }
      if (index == 2) {
        oldCoordinates[ringIndex][2] = vertex;
      }
      if (index == 3) {
        oldCoordinates[ringIndex][3] = vertex;
      }
    }

    p.setCoordinates(oldCoordinates);
  }

  static getRefLineCoords(coordinates, extent) {
    let y;
    if (coordinates.length !== 2) {
      return false;
    }
    y = coordinates[0][1];
    if (extent) {
      return [[extent[0], y], [extent[2], y]];
    } else {
      return [[-99999, y], [99999, y]];
    }
  }

  getRefLineCoords(extent) {
    let coordinates = this.getCoordinates()[0];
    let y;
    y = coordinates[0][1];
    if (extent) {
      return [[extent[0], y], [extent[2], y]];
    } else {
      return [[-99999, y], [99999, y]];
    }
  }

  getRefLine(extent, style) {
    const coords = this.getRefLineCoords(extent);
    this._referLineFeature = new Feature(new Line());
    this._referLineFeature.geometry.setCoordinates(coords);
    if (style) {
      this._referLineFeature.style = style;
    } else {
      this._referLineFeature.style = this.getDefaultStyleFunction()(this._referLineFeature);
      this._referLineFeature.style.splice(-1);
      this._referLineFeature.style.forEach(s => {
        s.style = "dash";
        s.color = [76, 189, 255];
      });
    }
    return this._referLineFeature;
  }

  /**
   * 获取默认绘制样式
   *
   * @method getDefaultStyleFunction
   * @returns {Function}
   */
  getDefaultStyleFunction() {
    const styles = Style.createDefaultEditing()
    return function (feature) {
      return styles[feature.geometry.geometryType]
    }
  }
}
